package com.cloud.bc.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcNotices;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.service.BcNoticesService;
import com.cloud.bc.service.BcSinglesService;
import com.cloud.bc.service.BcUsersService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("m/chat")
@AllArgsConstructor
public class ChatController extends BaseController {
    private final BcUsersService bcUsersService;
    private final BcNoticesService bcNoticesService;
    private final BcSinglesService bcSinglesService;

    /**
     * 消息
     */
    @GetMapping("")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request,"消息", "m/chat/index");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcNotices bcNotices = new BcNotices();
        bcNotices.setTargetId(userInfo.getId());
        bcNotices.setIsLook(0);
        IPage<BcNotices> bcNoticesIPage = bcNoticesService.selectPage(new Page().setCurrent(1).setSize(1), bcNotices);
        BcNotices bcNoticesResult = bcNoticesIPage.getRecords().stream().findFirst().orElse(new BcNotices());
        view.addObject("bcNotices", bcNoticesResult);
        return view;
    }

    /**
     * 消息
     */
    @GetMapping("{id}")
    public ModelAndView info(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable("id") Integer id) {
        ModelAndView view = baseView(request,"全部消息", "m/chat/info");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcSingles targetObj = bcSinglesService.selectOne(new BcSingles().setUserId(id));
        BcSingles bcSingles = bcSinglesService.selectOne(new BcSingles().setUserId(userInfo.getId()));
        view.addObject("title", bcSingles.getNickname());
        if (null != bcSingles) {
            JSONObject jsonSingle = new JSONObject();
            jsonSingle.put("user_id", bcSingles.getUserId());
            jsonSingle.put("photo", bcSingles.getPhoto());
            jsonSingle.put("pic", bcSingles.getPhoto());
            jsonSingle.put("nickname", bcSingles.getNickname());
            jsonSingle.put("check", bcSingles.getIsCheck());
            jsonSingle.put("close", bcSingles.getIsClose());
            view.addObject("userObj", jsonSingle.toString());
        }
        JSONObject jsonTarget = new JSONObject();
        jsonTarget.put("user_id", targetObj.getUserId());
        jsonTarget.put("photo", targetObj.getPhoto());
        jsonTarget.put("pic", targetObj.getPhoto());
        jsonTarget.put("nickname", targetObj.getNickname());
        jsonTarget.put("check", targetObj.getIsCheck());
        jsonTarget.put("close", targetObj.getIsClose());
        view.addObject("targetObj", jsonTarget.toString());
        view.addObject("userId", id);
        return view;
    }

    /**
     * 新朋友
     */
    @GetMapping("newfriend")
    public ModelAndView newfriend(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request,"新朋友", "m/chat/newfriend");
        return view;
    }
}
